<?php

abstract class Abstract_Keeporiel_Configured_DBConnection implements IKeeporiel_DBConnection
{
   const INI_DIRECTORY = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'ini' . DIRECTORY_SEPARATOR;
   protected $dbType;
   protected $connection;
   private $config;

   public function __construct()
   {
       $this->configure( $this->dbType );
       $this->initConnection();
   }

   public final function getConfig()
   {
       return $this->config;
   }

   public final function setConfig( $config )
   {
       $this->config = $config;
   }
   
   public function login( $username, $password )
   {
       $idQuery	= "( SELECT ID FROM BASIC_USERS WHERE USERNAME=:uid AND PASSWORD=:pass )";
   }
   
   protected final function configure( $type )
   {
      $config     = parse_ini_file( self::INI_DIRECTORY . 'dbsettings.ini' );
      $locFile    =  self::INI_DIRECTORY . $type . '_dbsettings.ini';
      if( file_exists( $locFile ) )
      {
         $locConfig = parse_ini_file( $locFile );
         // Any values in local config will overwrite the parent config file
         $config    = array_merge( $config, $locConfig ); 
      }
      $this->setConfig( $config );
   }

   protected function initConnection()
   {
      $this->connection = new PDO( $this->getConnectionString() );
   }

   protected function getConnectionString()
   {
      $config = $this->getConfig();
      return "{$config[ 'dbtype' ]}:dbname={$config[ 'dbname' ]};host={$config[ 'host' ]};user={$config[ 'user' ]};password={$config[ 'password' ]}";
   }
}
